
#include "stdafx.h"
#include "Win32.Diagnostics.Result.h"
#include "Win32.String.h"

// -----------------------------------------------------------------------------

namespace Kif
{
namespace Win32
{
namespace Diagnostics
{

void Result::ErrorNumsToString(Kif::Win32::String &message)
{
   TCHAR lpar[] = _T("(");
   TCHAR rpar[] = _T(")");
   if (LocalErrorNo != 0)
   {
      message.Concat(_T(" LocalErrorNo"));
      message.Concat(lpar);
      message.Concat((long)LocalErrorNo);
      message.Concat(rpar);
   }
   if (ErrorNo != 0)
   {
      message.Concat(_T(" ErrorNo"));
      message.Concat(lpar);
      message.Concat((long)ErrorNo);
      message.Concat(rpar);
   }
   if (HResult != 0)
   {
      message.Concat(_T(" HResult"));
      message.Concat(lpar);
      message.Concat((long)HResult);
      message.Concat(rpar);
   }
}  // end of ErrorNumsToString

/// <summary>Get error message based on ErrorNo and/or HRESULT values
/// note that the Caller is responsable for the disposition
/// of the string (ex. delete errMess;)</summary>
Kif::Win32::String *Result::GetErrorMessage()
{
   Kif::Win32::String *m = new Kif::Win32::String();
   if (m == NULL)
      return(NULL);

   // get error message based on the last error number
   if (ErrorNo != 0)
   {
      TCHAR *lpMsgBuf;
      FormatMessage( 
          FORMAT_MESSAGE_ALLOCATE_BUFFER | 
          FORMAT_MESSAGE_FROM_SYSTEM | 
          FORMAT_MESSAGE_IGNORE_INSERTS,
          NULL,
          ErrorNo,
          0, // Default language
          (LPTSTR) &lpMsgBuf,
          0,
          NULL 
      )  ;

      m->Copy(lpMsgBuf,Kif::Win32::String::GetLengthNonPrintable(lpMsgBuf));

      // Free the buffer.
      LocalFree( lpMsgBuf );
   }

   return(m);
}  // end of GetErrorMessage

}  // end of Kif::Win32::Diagnostics
}  // end of Kif::Win32
}  // end of Kif


